<template>
  <div class="xtx-pagination" v-if="total > 0">
    <a href="javascript:;" class="disabled" v-if="myCurrPage <= 1">上一页</a>
    <a @click="changePager(myCurrPage - 1)" href="javascript:;" v-else>上一页</a>
    <span v-if="pager.startPage > 1">...</span>
    <a @click="changePager(item)" href="javascript:;" v-for="item in pager.btnArr" :key="item" :class="{ active: myCurrPage === item }">{{ item }}</a>
    <span v-if="pager.endPage < pager.totalPage">...</span>
    <a href="javascript:;" :class="disaled" v-if="myCurrPage >= pager.totalPage">下一页</a>
    <a @click="changePager(myCurrPage + 1)" href="javascript:;" v-else>下一页</a>
  </div>
</template>
<script>
import { ref, computed, watch } from 'vue'
export default {
  name: 'XtxPagination',
  props: {
    // 总评论数,当前页码,每页多少数据
    total: {
      type: Number,
      default: 100
    },
    pageSize: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  setup(props, { emit }) {
    // 需要数据
    // 1.约定按钮的个数  5个  如果成为动态的需要设置响应式数据
    const count = 5
    // 2.当前的页码
    const myCurrPage = ref(5)
    // 3.总的页数 = 总条数 / 每页条数  向上取整
    const myTotal = ref(100)
    const myPageSize = ref(10)
    // 其他数据(总页数,起始按钮,结束按钮，按钮数组)依赖上面的数组得到
    const pager = computed(() => {
      // 总页数
      const totalPage = Math.ceil(myTotal.value / myPageSize.value)
      // 起始按钮  结束按钮  正常情况下
      const offset = Math.floor(count / 2)
      let startPage = myCurrPage.value - offset
      let endPage = startPage + count - 1
      if (startPage < 1) {
        startPage = 1
        endPage = startPage + count - 1 > totalPage ? totalPage : startPage + count - 1
      }
      if (endPage > totalPage) {
        endPage = totalPage
        startPage = endPage - count + 1 < 1 ? 1 : endPage - count + 1
      }
      const btnArr = []
      for (let i = startPage; i <= endPage; i++) {
        btnArr.push(i)
      }
      return {
        startPage,
        endPage,
        btnArr,
        totalPage
      }
    })
    //  count 按钮的个数 --- mycurrentPage 当前页码 --- myTotal 数据的总条数 --- myPageSize 每一页有多少条数据
    //  startPage 起始页 = 1 --- endPage 结束页  就是总的页数
    //  通过watcher来监听页码的变化
    watch(
      props,
      () => {
        myCurrPage.value = props.currentPage
        myTotal.value = props.total
        myPageSize.value = props.pageSize
      },
      { immediate: true }
    )

    // 当点击页码的时候更新评论的数据
    const changePager = (page) => {
      myCurrPage.value = page
      emit('current-change', page)
    }

    return { pager, myCurrPage, changePager }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333;
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>
